package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.UserRelation;
import com.ruoyi.system.enums.RoleEnums;
import com.ruoyi.system.mapper.UserRelationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysVisitor;
import com.ruoyi.system.service.ISysVisitorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 来客访记Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/visitor")
@RequiredArgsConstructor
public class SysVisitorController extends BaseController
{

    private final ISysVisitorService sysVisitorService;
    private final UserRelationMapper userRelationMapper;

    /**
     * 查询来客访记列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysVisitor sysVisitor)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (!loginUser.getUser().isAdmin()){
            List<SysRole> roles = loginUser.getUser().getRoles();
            //老人只能看到他自己的访客记录
            if (!roles.isEmpty() && roles.stream().anyMatch(m -> RoleEnums.elder.getValue().equals(m.getRoleKey()))){
                sysVisitor.setElderUserId(loginUser.getUserId());
            }
            //家属只能看到自己家属的记录
            if (!roles.isEmpty() && roles.stream().anyMatch(m -> RoleEnums.family.getValue().equals(m.getRoleKey()))){
                UserRelation userRelation = new UserRelation();
                userRelation.setFamilyUserId(loginUser.getUserId());
                List<UserRelation> userRelations = userRelationMapper.selectUserRelationList(userRelation);
                if (!userRelations.isEmpty()){
                    Set<Long> collect = userRelations.stream().map(UserRelation::getElderUserId).collect(Collectors.toSet());
                    sysVisitor.setElderUserIds(collect);
                }
            }
        }
        startPage();
        List<SysVisitor> list = sysVisitorService.selectSysVisitorList(sysVisitor);
        return getDataTable(list);
    }

    /**
     * 导出来客访记列表
     */
    @Log(title = "来客访记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SysVisitor sysVisitor)
    {
        List<SysVisitor> list = sysVisitorService.selectSysVisitorList(sysVisitor);
        ExcelUtil<SysVisitor> util = new ExcelUtil<SysVisitor>(SysVisitor.class);
        util.exportExcel(response, list, "来客访记数据");
    }

    /**
     * 获取来客访记详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysVisitorService.selectSysVisitorById(id));
    }

    /**
     * 新增来客访记
     */
    @Log(title = "来客访记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysVisitor sysVisitor)
    {
        return toAjax(sysVisitorService.insertSysVisitor(sysVisitor));
    }

    /**
     * 修改来客访记
     */
    @Log(title = "来客访记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysVisitor sysVisitor)
    {
        return toAjax(sysVisitorService.updateSysVisitor(sysVisitor));
    }

    /**
     * 删除来客访记
     */
    @Log(title = "来客访记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysVisitorService.deleteSysVisitorByIds(ids));
    }

    /**
     * 根据老人ID查询访客记录
     */
    @GetMapping("/elder/{elderUserId}")
    public AjaxResult getVisitorsByElderId(@PathVariable("elderUserId") Long elderUserId)
    {
        return success(sysVisitorService.selectSysVisitorByElderUserId(elderUserId));
    }
} 